

#import "TableViewController.h"
#import "AnnouncementDdetailsViewController.h"
#import "AFNetworking.h"
#import "AnnouncementmaiModel.h"
#import "TableViewCell.h"
@interface TableViewController ()

@end

@implementation TableViewController
{
    NSMutableArray *_announcementmaiArray;

}


-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Web_Msg" forKey:@"username"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

//        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

        _announcementmaiArray = [[NSMutableArray alloc]init];

        for (NSDictionary *modeldic in [dic objectForKey:@"HX_Web_Msg"]) {

        AnnouncementmaiModel *model = [[AnnouncementmaiModel alloc]init];
        [model setValuesForKeysWithDictionary:modeldic];
        [_announcementmaiArray addObject:model];

        }

        [self.tableView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    [self obtainData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _announcementmaiArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier1=@"cell";
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    AnnouncementmaiModel *model = [_announcementmaiArray objectAtIndex:indexPath.row];

    cell.time1Label.text = [NSString stringWithFormat:@"%@",[model.W_M_AddTime substringWithRange:NSMakeRange(5, 2)]];

    cell.time2Label.text = [NSString stringWithFormat:@"%@",[model.W_M_AddTime substringWithRange:NSMakeRange(8, 2)]];
    cell.titleLabel.text = model.W_M_Title;

    return cell;
}

//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    AnnouncementDdetailsViewController *aDVC= [[AnnouncementDdetailsViewController alloc] init];
    aDVC.model = [_announcementmaiArray objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:aDVC animated:YES];
//    NSLog(@"qwe");
}

@end
